package cn.wolfcode.web.interceptor;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckLoginInterceptor implements HandlerInterceptor {
@Autowired
IEmployeeService employeeService;
   //这个方法是在访问之前拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果是静态资源就以DefaultServlet结尾
        //如果是动态资源就以HandlerMethod结尾
        if (handler instanceof HandlerMethod){
            Employee emp = (Employee)request.getSession().getAttribute("USER_IN_SESSION");

            if (emp==null){

                response.sendRedirect("/login.html");
                return false;
            }

        }
        return true;
    }
}
